<template>
	<view class="indexs">
		<view class="header header_wi" :style="'padding-top: ' + navH + 'px;'">
			<view class="header_height"
				:style="'height:' + gaodu + 'px;line-height:' + gaodu + 'px;top:' + gaodutops + 'px;'">
				<view class="index_h_cont3">
					<view class="index_wenben_img" @tap="jumpzuo">
						<image :src="imgUrl + '/dzx_img33.png'" mode="widthFix"></image>
					</view>
					<view class="index_wenben">语言</view>
				</view>
			</view>
			<view class="coloe_title">s</view>
		</view>
		<view class="index_center" :style="'padding-top:' + (navH + 50) + 'px;'">
			<view class="cell">
				<view v-for="(item, index) in langs" :key="index" class="list" @click="switchLanguage(item)">
					{{ item.name }}
				</view>
			</view>
		</view>
	</view>
</template>
<script>
	const app = getApp();
	let request = require('../../utils/require');

	export default {
		data() {
			return {
				navH: app.globalData.navHeight,
				gaodu: app.globalData.height,
				gaodutops: app.globalData.top,
				imgUrl: app.globalData.imgUrl,
				langs: [
					// {
					// 	name: '简体',
					// 	type: 'zhHans'
					// },
					// {
					// 	name: 'English',
					// 	type: 'en'
					// },
					// {
					// 	name: '繁體',
					// 	type: 'zhHant'
					// }
				]
			};
		},
		onLoad() {
			this.getLangSetting();
		},
		methods: {
			switchLanguage(e) {
				const type = e.type;
				console.log(this.$i18n.locale,987)
				if (this.$i18n.locale === type) return;
				const that = this;
				uni.showLoading({
					title: '......',
					mask: true
				});
				request
					.post(`index.php?s=/api/page/getLangPackage&wxapp_id=` + uni.getStorageSync('web_id'), {
						type: type
					})
					.then(res => {
						if (res.data.code == 1) {
							uni.hideLoading();
							//动态插入内容到i18n
							that.$i18n.setLocaleMessage(type, res.data.data);
							//本地语言开关切换
							that.$i18n.locale = type;
							//设置本地存储
							uni.setStorageSync("lang",type);
							uni.setStorageSync('langData',res.data.data)
							that.jumpzuo();
						}
						uni.hideLoading();
					});
			},
			jumpzuo() {
				const pages = getCurrentPages();
				if (pages.length > 1) {
					uni.navigateBack();
				} else {
					// #ifdef H5
					history.back();
					// #endif
				}
			},
			getLangSetting() {
				var that = this;
				// api/page/getLangSetting&wxapp_id=10001
				uni.showLoading({});
				request.post(`index.php?s=/api/page/getLangSetting&wxapp_id=` + uni.getStorageSync('web_id'), {}).then(
					res => {
						if (res.data.code == 1) {
							that.langs= res.data.data.data
						}
						uni.hideLoading();
					});
			}
		}
	};
</script>
<style scoped>
	.cell {
		padding: 0 30rpx;
	}

	.cell .list {
		padding: 15rpx 0;
		border-bottom: 1rpx solid #eee;
		font-size: 32rpx;
	}
</style>